package com.maxmlr.music.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.maxmlr.music.song.dao.SongMapper;
import com.maxmlr.music.song.entity.Song;
import com.maxmlr.music.system.entity.Files;
import com.maxmlr.music.system.service.FilesService;
import com.maxmlr.music.user.entity.UserInfo;
import com.maxmlr.music.user.entity.UserSongEstimate;
import com.maxmlr.music.user.dao.UserSongEstimateMapper;
import com.maxmlr.music.user.param.UserSongEstimateInParam;
import com.maxmlr.music.user.param.UserSongEstimateOutParam;
import com.maxmlr.music.user.service.UserInfoService;
import com.maxmlr.music.user.service.UserSongEstimateService;
import com.maxmlr.music.common.base.BaseServiceImpl;
import com.maxmlr.music.user.service.transfer.UserSongEstimateTransfer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author max
 * @version 1.0
 * @ClassName UserSongEstimateServiceImpl
 * @Description  用户歌曲评论信息表  服务实现类
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-14
 */
@Slf4j
@Service
public class UserSongEstimateServiceImpl extends BaseServiceImpl<UserSongEstimateMapper, UserSongEstimate> implements UserSongEstimateService {

    @Autowired
    private UserSongEstimateMapper mapper;

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private FilesService filesService;

    @Autowired
    private UserSongEstimateTransfer transfer;

    @Override
    public Page<UserSongEstimateOutParam> listUserSongEstimate(Page<UserSongEstimateOutParam> page, UserSongEstimateInParam inParam) {
        mapper.selectUserSongEstimateList(page, inParam);
        List<UserSongEstimateOutParam> list = page.getRecords();
        if (Objects.isNull(list) || list.isEmpty()) {
            return page;
        }

        Set<Integer> userIds = list.stream()
                .map(UserSongEstimateOutParam::getUserId)
                .collect(Collectors.toSet());
        Map<Integer, UserInfo> userInfoMap = Optional.ofNullable(userInfoService.listByIds(userIds))
                .orElse(Lists.newArrayList())
                .stream()
                .collect(Collectors.toMap(UserInfo::getId, p -> p));
        Set<Integer> headImgIds = userInfoMap.values().stream()
                .map(UserInfo::getHeadImgId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Integer, String> headImgMap = Optional.ofNullable(filesService.listByIds(headImgIds))
                .orElse(Lists.newArrayList())
                .stream()
                .collect(Collectors.toMap(Files::getId, Files::getUrl));

        page.getRecords().forEach(p -> transfer.fullUserInfo(p, userInfoMap, headImgMap));
        return page;
    }

    @Override
    public UserSongEstimateOutParam saveEstimate(UserSongEstimateInParam inParam) {
        UserInfo userInfo = userInfoService.getRequestUser();
        Files files = filesService.getById(userInfo.getHeadImgId());

        Song song = songMapper.selectById(inParam.getSongId());

        UserSongEstimate userSongEstimate = transfer.po2UserSongEstimate(inParam, userInfo);
        save(userSongEstimate);

        return transfer.po2UserSongEstimateOutParam(userSongEstimate, userInfo, song, files);
    }
}
